using System.Html;
using System.Runtime.CompilerServices;
using Awaiter;
using Nsb;
using Nsb.Classes;
using Yoshi.Classes;
using jQueryApi;

namespace MinionsEdit.Classes
{
    [IgnoreNamespace]
    public static class MiPageGear
    {
        private static bool _pageGearIsOn;

        public static void InstallPageGear()
        {
            if (_pageGearIsOn)
                return;
            _pageGearIsOn = true;
            BuildPageGear();
        }

        private static void BuildPageGear()
        {
            new Await()
                .AddDx(
                    delegate
                    {
                        MinionEdit focused = MinionEdit.ActiveFocus;
                        bool hasFocus = focused != null;

                        jQueryObject pageGearContainer = jQuery.FromHtml("<div id='PageGearContainer'/>").AppendTo(Document.Body);

                        jQueryObject gearsOuter = jQuery.FromHtml("<div id='GearsBtnOuter' class='GearsBtnOuter'/>")
                            .AppendTo(pageGearContainer)
                            ;

                        jQueryObject gear = jQuery.FromHtml("<img id='GearsBtn' class='GearsBtn'/>")
                            .Attribute("src", Uri.Img("Common/gear16.gif"))
                            .AppendTo(gearsOuter)
                            ;

                        DDict mu = new DDict();

                        if (Storage.GetLocal("CopyBuffer") != null)
                            mu.Add("&#9679; Paste to this page", PasteEv);
                        if (hasFocus && Undo.HasPrior(focused.PartitionKey))
                            mu.Add("&#9679; <b>&#8656; Undo</b> - <small>(ctrl-z)</small>", UndoEv);
                        if (hasFocus && Undo.HasNext(focused.PartitionKey))
                            mu.Add("&#9679; <b>Redo &#8658;</b> - <small>(ctrl-alt-z)</small>", RedoEv);

                        mu
                            .Add("addablesBar", "<b>Insert</b>")
                            .Add("&#9679; Text", AddTextItemEv)
                            /*
                            .Add("&#9679; Numbered list", delegate(jQueryEvent e, string item) { HtmlOrderedList(); })
                            .Add("&#9679; Button", delegate(jQueryEvent e, string item) { InsertButton(); })
                            .Add("&#9679; Single-line form box", delegate(jQueryEvent e, string item) { InsertTextBox(); })
                            .Add("&#9679; Multi-line form box", delegate(jQueryEvent e, string item) { InsertTextareaBox(); })
                            .Add("&#9679; Page links box", delegate(jQueryEvent e, string item) { InsertLinks(); })
                            .Add("&#9679; Embedded html code", delegate(jQueryEvent e, string item) { AskEmbeddedCode(); })
                             */
                            .Add("commandsBar", "<b></b>")
                            .Add("&#9679; Add images", UploadImgEv)
                            //.Add("&#9679; Admin page", delegate(jQueryEvent e) { Script.Literal("Master.GoPage({0},{1})", ThemeYoshi.DefaultPageClass, ThemeYoshi.AdminPageName); })
                            .Add("&#9679; Manage pages", PagesEv)
                            .Add("&#9679; Copy whole page", CopySlideEv)
                            .Add("&#9679; Change page colors", ColorizeBgEv)
                            .Add("&#9679; Change page background", AddNewBgEv)
                            .Add("publishBar", "<b></b>")
                            //.Add("&#9679; Go to published page", ThemeYoshi.ToPublished)
                            .Add("&#9679; Turn curating off", CuratingOffEv)
                            ;

                        new PopMenu()
                            .AddList(mu.End())
                            .WrapAround(gear)
                            .AddClass("EditGears")
                            ;
                    })
                .Commit();
        }

        private static void RedoEv(jQueryEvent e)
        {
            if (MinionEdit.FocusOrFlash)
                MinionEdit.ActiveFocus.RedoEv(e);
        }

        private static void UndoEv(jQueryEvent e)
        {
            if (MinionEdit.FocusOrFlash)
                MinionEdit.ActiveFocus.UndoEv(e);
        }

        private static void PasteEv(jQueryEvent e)
        {
            if (MinionEdit.CanvasOrFlash)
                MiCanvasEdit.FlashWindowEv(e);
        }

        private static void AddTextItemEv(jQueryEvent e)
        {
            if (MinionEdit.CanvasOrFlash)
                MiCanvasEdit.FlashWindowEv(e);
        }

        private static void UploadImgEv(jQueryEvent e)
        {
            if (MinionEdit.CanvasOrFlash)
                MiCanvasEdit.FlashWindowEv(e);
        }

        private static void PagesEv(jQueryEvent e)
        {
            if (MinionEdit.CanvasOrFlash)
                MiCanvasEdit.FlashWindowEv(e);
        }

        private static void CopySlideEv(jQueryEvent e)
        {
            if (MinionEdit.CanvasOrFlash)
                MiCanvasEdit.FlashWindowEv(e);
        }

        private static void ColorizeBgEv(jQueryEvent e)
        {
            if (MinionEdit.CanvasOrFlash)
                MiCanvasEdit.FlashWindowEv(e);
        }

        private static void AddNewBgEv(jQueryEvent e)
        {
            if (MinionEdit.CanvasOrFlash)
                MiCanvasEdit.FlashWindowEv(e);
        }

        private static void CuratingOffEv(jQueryEvent e)
        {
            if (MinionEdit.CanvasOrFlash)
                MiCanvasEdit.FlashWindowEv(e);
        }
    }
}